package com.qyhl.module_practice.activity.detail.comment;


import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeCommentBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerBean;

import java.util.List;

public class PracticeActCommentModel implements PracticeActCommentContract.PracticeActCommentModel {
    private PracticeActCommentPresenter mPresenter;

    PracticeActCommentModel(PracticeActCommentPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getCommentList(String actId, final String page) {

        EasyHttp.get(PracticeUrl.GET_PRACTICE_ACT_COMMENT_LIST)
                .params("actId", actId)
                .params("page", page)
                .execute(new SimpleCallBack<List<PracticeCommentBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setCommentError("暂无更多评论！", !page.equals("1"));
                        } else {
                            mPresenter.setCommentError("获取列表失败，请稍后再试！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeCommentBean> practiceCommentBeans) {
                        if (practiceCommentBeans != null && practiceCommentBeans.size() > 0) {
                            mPresenter.setCommentList(practiceCommentBeans, !page.equals("1"));
                        } else {
                            mPresenter.setCommentError("暂无更多评论！", !page.equals("1"));
                        }
                    }
                });
    }

    @Override
    public void addComment(String actId, String phone, String content) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_ACT_ADD_COMMENT)
                .params("actId", actId)
                .params("phone", phone)
                .params("content", content)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.commentError("评论失败，请稍后重试!");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.commentSuccess("评论成功！");
                        } else {
                            mPresenter.commentError("评论失败，请稍后重试!");
                        }
                    }
                });
    }

    @Override
    public void getVolInfo(String phone) {

        EasyHttp.post(PracticeUrl.GET_PRACTICE_VOL_SEARCH)
                .params("siteId", CommonUtils.getInstance().getSite()+"")
                .params("phone", phone)
                .params("page", "1")
                .execute(new SimpleCallBack<List<PracticeVolunteerBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setVolInfo(null);
                    }

                    @Override
                    public void onSuccess(List<PracticeVolunteerBean> practiceVolunteerBeans) {
                        if (practiceVolunteerBeans != null && practiceVolunteerBeans.size() > 0) {
                            mPresenter.setVolInfo(practiceVolunteerBeans.get(0));
                        } else {
                            mPresenter.setVolInfo(null);
                        }
                    }
                });
    }
}
